class User < ActiveRecord::Base
  acts_as_authentic
  has_many :forums
  has_many :forum_threads
  has_many :posts
  has_many :reviews, :dependent => :destroy
  has_many :edits, :order => "created_at DESC" 
  has_many :assignments, :dependent => :destroy
  has_many :roles, :through => :assignments
  has_many :favorite_lists, :dependent => :destroy
  has_many :favorites, :through => :favorite_lists

  #Named Scopes
  #named_scope :all_admin, :conditions => {:assignment => Role.admin_role}
  #named_scope :all_moderators

  #Callbacks
  before_create :default_favorites

  #Protection against mass assignment
  attr_accessible :username, :email, :password, :password_confirmation

  #
  def deliver_password_reset_instructions!
    reset_perishable_token!
    Confirmer.deliver_password_reset_instructions(self)
  end

  #
  def activate!
    self.active = true
    save
  end

  #
  def deliver_activation_instructions!
    reset_perishable_token!
    Confirmer.deliver_activation_instructions(self)
  end

  #
  def deliver_welcome!
    reset_perishable_token!
    Confirmer.deliver_welcome(self)
  end

  #
  def role_symbols
    roles.map do |role|
      role.name.underscore.to_sym
    end
  end

  #
  def default_favorites_list
    favorite_lists.all(:conditions => {:name => 'Default'})
  end

  private

  def default_favorites
    self.favorite_lists.build(:name => 'Default')
  end
end
